package com.cheng.web.action.base;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.cheng.bos.actionUtils.ActionUtils;
import com.cheng.domain.base.Courier;
import com.cheng.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;


@Namespace("/courier")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Actions
public class CourierAction extends ActionUtils<Courier> {
	
	@Autowired
	private CourierService courierService;
	@Action(value="addcourier",results={@Result(name="success",location="../pages/base/courier.html",type="redirect")})
	public String addCourier(){
		System.out.println(11);
		courierService.addCourier(model);
		return SUCCESS;
	}

	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}


	@Action(value="delcourier",results={@Result(name="success",location="../pages/base/courier.html",type="redirect")})
	public String delCourier(){
		if (ids != "," && !"".equals(ids) ) {
			String[] str = ids.split(",");
			courierService.delCourier(str);
		}
		return SUCCESS;
	}
	@Action(value="resumecourier",results={@Result(name="success",location="../pages/base/courier.html",type="redirect")})
	public String resumeCourier(){
		if (ids != "," && !"".equals(ids)) {
			String[] str = ids.split(",");
			courierService.resumeCourier(str);
		}
		return SUCCESS;
	}
	
	
	@Action(value="findallcourier",results={@Result(name="success",type="json")})
	public String findCourierByPage(){
		//得到分页实体的对象
		Pageable pageable = new PageRequest(page-1, rows);
		//获取查询条件
		Specification<Courier> specification = new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate predicate1 = cb.like(root.get("courierNum").as(String.class), "%"+model.getCourierNum()+"%");
					list.add(predicate1);
				} 
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate predicate2 = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(predicate2);
				}
				if(StringUtils.isNotBlank(model.getType())){
					Predicate predicate3 = cb.like(root.get("type").as(String.class),"%"+ model.getType()+"%");
					list.add(predicate3);
				}
				Join<Object, Object> standardJoin = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && StringUtils.isNotBlank(model.getStandard().getName())) {
					Predicate predicate4 = cb.like(standardJoin.get("name").as(String.class), "%"+model.getStandard().getName()+"%");
					list.add(predicate4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		//调用业务层进行分页查找得到分页查询结果对象
		Page<Courier> list = courierService.findAllCourierBypage(pageable,specification);
		//将查询结果封装至值栈顶层,会自动转化为json
		pushPageDataToValueStack(list);
		return SUCCESS;
	}
	
	@Action(value="getnoassociation",results={@Result(name="success",type="json")})
	public String getCourierNOassociation(){
		List<Courier> list = courierService.findNOassiocationCourier();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
